﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.Maiyoule.GsmSms.Components {
    /// <summary>
    /// 短信中心号码
    /// </summary>
    class ServiceCenterAddress {

        private AddressType type;
        /// <summary>
        /// 短信中心地址的类型 81：指国内的号码 91：指国际的号码  91最常用
        /// </summary>
        public AddressType Type {
            get { return type; }
            set { type = value; }
        }

        private string address = "00";
        /// <summary>
        /// 短信中心号码地址 默认为00
        /// </summary>
        public string Address {
            get { return address; }
            set { address = value; }
        }

        /// <summary>
        /// 获取服务中心号码 （编码后的16进制）
        /// </summary>
        /// <returns></returns>
        public string GetServiceCenterAddress() {

            if (string.IsNullOrWhiteSpace(this.address)) {
                return null;
            }
            string tmpaddress = this.address;
            //去掉开头的+
            tmpaddress = tmpaddress.TrimStart('+');
            //支持国际号码？ 暂时只支持中国
            if (tmpaddress.Substring(0, 2) != "86") {
                tmpaddress = "86" + tmpaddress;
            }
            //号码翻转
            tmpaddress = string.Format("{0}{1}", this.type.GetAddressType(), Util.ParityChange(tmpaddress));
            string len = Util.Otc2HexString(tmpaddress.Length / 2);

            return len + tmpaddress;
        }

        /// <summary>
        /// 设置中心号码数据 
        /// </summary>
        /// <param name="centerstr">16进制编码的数据</param>
        public void SetServiceCenterAddress(string centerstr) {
            if (string.IsNullOrWhiteSpace(centerstr)) {
                return;
            }

            //类型
            string typestr = centerstr.Substring(0, 2);

            this.type.SetAddressType(typestr);

            //号码
            string numstr = centerstr.Substring(2);
            //互换
            numstr = Util.ParityChange(numstr);

            numstr = "+" + numstr;
            numstr = numstr.TrimEnd('F');
            this.address = numstr;

        }

        /// <summary>
        /// 初始化短信中心号码
        /// </summary>
        public ServiceCenterAddress() {
            //默认配置
            this.type = new AddressType();
        }
    }
}
